local DIY = require "packages/diy_utility/diy_utility"

local weiyaozhirong = fk.CreateSkill{
    name = "ym2__weiyaozhirong",
    tags = {DIY.ReadySkill},
}
Fk:loadTranslationTable{
    ["ym2__weiyaozhirong"] = "未耀之荣",
    [":ym2__weiyaozhirong"] = "蓄势技，此技能初始视为已发动。出牌/弃牌阶段结束时，你获得每名角色各一张牌并展示。若其中没有【杀】，你可以失去1点体力，重置此技能使用次数。",
    ["#ym2__weiyaozhirong"] = "未耀之荣：获得%dest的一张牌",
    ["#ym2__weiyaozhirong-update"] = "未耀之荣：你可以失去1点体力，重置此技能",

    ["$ym2__weiyaozhirong1"] = "我仍然……听得见那声叹息。",
    ["$ym2__weiyaozhirong2"] = "下雨了。",
}

weiyaozhirong:addEffect(fk.EventPhaseEnd, {
    can_trigger = function (self, event, target, player, data)
        if player == target and player:hasSkill(weiyaozhirong.name) and (player.phase == Player.Play or player.phase == Player.Discard) then
            return table.find(player.room.alive_players, function (cp)
                return (player == cp and #player:getCardIds("e") > 0) or (player ~= cp and not cp:isNude())
            end)
        end
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local gains = {}
        for _, p in ipairs(room.alive_players) do
            local cards = p:getCardIds("he")
            if p == player then cards = p:getCardIds("e") end
            if #cards > 0 then
                local card = room:askToChooseCard(player, {
                    target = p,
                    flag = p == player and "e" or "he",
                    skill_name = weiyaozhirong.name,
                    prompt = "#ym2__weiyaozhirong::"..p.id,
                })
                table.insertIfNeed(gains, card)
            end
        end
        room:obtainCard(player, gains, false, fk.ReasonJustMove, player, weiyaozhirong.name)
        room:showCards(gains, player)
        if not table.find(gains, function (cardID)
            return Fk:getCardById(cardID).trueName == "slash"
        end) then
            if room:askToSkillInvoke(player, {
                skill_name = weiyaozhirong.name,
                prompt = "#ym2__weiyaozhirong-update"
            }) then
                room:loseHp(player, 1, weiyaozhirong.name)
                DIY.resetReadySkill(player, weiyaozhirong.name, weiyaozhirong.name)
            end
        end
    end,
})

weiyaozhirong:addAcquireEffect(function (self, player, is_start)
    DIY.setReadySkill(player, weiyaozhirong.name, weiyaozhirong.name, true)
end)

return weiyaozhirong